package org.ladybug.web.css;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

public class CssServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String uri = req.getRequestURI();
		String context = req.getContextPath();
		uri = uri.substring(uri.lastIndexOf(context)+context.length()+1);
		uri = new StringBuffer("/WEB-INF/"+uri).toString();
		InputStream cssFile = getServletContext().getResourceAsStream(uri);
		byte[] b = IOUtils.toByteArray(cssFile);
		System.out.println("CssServlet.doGet() --> URI: "+uri);
		System.out.println("CssServlet.doGet() --> CSSFile: "+cssFile);
		OutputStream outStream = resp.getOutputStream();
		outStream.write(b);
		outStream.flush();
		outStream.close();
	}

}
